﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain.MainModule.Entities;
using Presentation.Web.MVC.Client.ViewModels;
using System.Web.Routing;
using Application.MainModule.EmployeesManagement;

namespace Presentation.Web.MVC.Client.Controllers
{
    /// <summary>
    /// This controller handles all the actions related to a employee.
    /// </summary>
    [HandleError]
    public class EmployeeController : Controller
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="EmployeeController"/> class.
        /// </summary>
        /// <param name="employeeService">The employee service.</param>
        public EmployeeController(IEmployeeManagementService employeeService)
        {
            _employeeService = employeeService;
        }

        #endregion

        #region Members

        /// <summary>
        /// Instance of the employee service.
        /// </summary>
        private IEmployeeManagementService _employeeService;

        /// <summary>
        /// Display a paged listing of the existing employees.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">The page size.</param>
        /// <returns></returns>
        public ActionResult Index(int page, int pageSize)
        {

            IList<Employee> employees = _employeeService.FindPagedEmployees(page, pageSize);
            return View(new EmployeeListViewModel(employees, page, pageSize));
        }

        /// <summary>
        /// Shows a specific employee in detail.
        /// </summary>
        /// <param name="employeeId">The employee code.</param>
        /// <returns>A view of the employee details.</returns>
        public ActionResult Details(Guid employeeId)
        {
            //Get the employee aggregate.
            Employee employee = _employeeService.FindEmployeeById(employeeId);
            //Display its details.
            return View(employee);
        }


        /// <summary>
        /// Displays a form to create a new Employee.
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// Creates the specified employee based on the request data and displays it.
        /// </summary>
        /// <param name="employee">The employee to add.</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(Employee employee)
        {
            //Check if there aren't validation errors on the employee.
            if (ModelState.IsValid)
            {
                //Add the employee to the data base.
                _employeeService.AddEmployee(employee);
                //Show the fist page of the employees list.
                return RedirectToAction("Index", new { page = 0, pageSize = 10 });
            }
            else
            {
                //Return the Create view and display the validation errors.
                return View();
            }
        }


        /// <summary>
        /// Displays a form to edit the employee.
        /// </summary>
        /// <param name="employeeId">The employee code.</param>
        /// <returns></returns>
        public ActionResult Edit(Guid employeeId)
        {
            //Get the employee aggregate to edit.
            Employee employee = _employeeService.FindEmployeeById(employeeId);
            //Return the
            return View(employee);
        }


        /// <summary>
        /// Saves the changes on the editions performed to the specified employee.
        /// </summary>
        /// <param name="employee">The employee.</param>
        /// <returns>The details of the employee.</returns>
        [HttpPost]
        public ActionResult Edit(Employee employee)
        {
            if (ModelState.IsValid)
            {
                _employeeService.ChangeEmployee(employee);
                return RedirectToAction("Details", new RouteValueDictionary() { { "employeeId", employee.UserId } });
            }
            else
            {
                return View();
            }
        }


        /// <summary>
        /// Deletes the specified employee code.
        /// </summary>
        /// <param name="employeeId">The employee code.</param>
        /// <returns>The details of the employee.</returns>
        [HttpDelete]
        public ActionResult Delete(Guid employeeId)
        {
            Employee employee = _employeeService.FindEmployeeById(employeeId);
            _employeeService.RemoveEmployee(employee);
            return RedirectToAction("Index", new RouteValueDictionary() { { "page", 0 }, { "pageSize", 10 } });
        }

        #endregion

    }
}